package com.tenement.controller.backend;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.tenement.base.constants.SysmConstants.state;
import com.tenement.base.exception.ParameterException;
import com.tenement.mybatis.entity.Admin;
import com.tenement.service.interfaces.IAdminService;

/**
 * 管理员登录
 * 
 * @author wk
 *
 */
@Controller
public class LoginController {

	private static final Log logger = LogFactory.getLog(LoginController.class);

	@Autowired
	private IAdminService iAdminService;

	/**
	 * 后台系统登录页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public String login() {
		logger.info("【-进入登录页面-】");
		return "view/backend_system/admin_login";
	}

	/**
	 * 登录验证
	 * 
	 * @param a
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "/login2.action")
	public String loginAction(String a_account,String a_password,HttpServletRequest request) {
		logger.info("【-后台管理页面-】" + a_account + " " + a_password);
		HttpSession session = request.getSession();
		try {
			if ("".equals(a_account) || a_account == null || "".equals(a_password) || a_password == null) {
				throw new ParameterException("【后台登录参数为空】");
			}
			Admin admin = iAdminService.loginAdmin(new Admin(a_account, a_password));
			if(admin==null){
				request.setAttribute("accountFlag",state.Failed);
				return "view/backend_system/admin_login";
			}else{
				session.setAttribute("adminLoginDetail",admin);
				return "view/backend_system/index";
			}
		} catch (ParameterException p) {
			p.printStackTrace();
		}
		return "view/backend_system/admin_login";
	}

}
